package com.daocheng.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.daocheng.weblog.common.domain.dos.StatisticsArticlePVDO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.time.LocalDate;
import java.util.List;

/**
 * @ClassName StatisticsArticlePVMapper
 * @author: 稻城
 * @date: 2025/3/16
 * @Version 1.0
 * @description: TODO
 */
public interface StatisticsArticlePVMapper extends BaseMapper<StatisticsArticlePVDO> {
    /**
     * @param date
     * @return int
     * @author 稻城
     * @Date 2025/3/16 17:22
     * @description 对指定日期的文章PV访问量进行+1
     */
    default int increasePVCount(LocalDate date) {
        return update(null, Wrappers.<StatisticsArticlePVDO>lambdaUpdate()
                .setSql("pv_count = pv_count + 1")
                .eq(StatisticsArticlePVDO::getPvDate, date));
    }

    default List<StatisticsArticlePVDO> selectLatestWeekRecords() {
        return selectList(Wrappers.<StatisticsArticlePVDO>lambdaQuery()
                .le(StatisticsArticlePVDO::getPvDate, LocalDate.now().plusDays(1))//小于明天
                .orderByDesc(StatisticsArticlePVDO::getPvDate).last("limit 7")
        );
    }
}